package com.reyco1.medusa.services.bitly
{
	import com.reyco1.medusa.abstract.SmartEventDispatcher;
	import com.reyco1.medusa.core.IDisposable;
	import com.reyco1.medusa.data.DataFormatter;
	import com.reyco1.medusa.loading.DataLoader;
	import com.reyco1.medusa.loading.DisplayLoader;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.utils.Dictionary;
	
	public class Bitly extends SmartEventDispatcher implements IDisposable
	{
		private const KEY:String   	 = "R_e89c2fc07e2a506bbbd1420ad73a0292";
		private const REST:String	 = "http://api.bitly.com";
		private const LOGIN:String 	 = "reyco1";
		private const FORMAT:String	 = "xml";
		private const QRCODE:String  = ".qrcode";
		private const SHORTEN:String = "/v3/shorten";
		private const EXPAND:String  = "/v3/expand";
		
		private var dataLoader:DataLoader;
		private var qrCodeLoader:DisplayLoader;
		private var url:String;
		private var properties:Object;
		private var currentRequestType:String;
		private var getQRImage:Boolean;
		private var tempObject:Object;
		
		private var dataReturns:Dictionary = new Dictionary();
		
		public function Bitly(properties:Object = null)
		{
			super();
			this.properties 	 = properties;
			dataReturns[SHORTEN] = "data";
			dataReturns[EXPAND]  = "data.entry";
		}
		
		public function shorten(url:String, fetchQRCode:Boolean = false):void
		{
			currentRequestType = SHORTEN;
			getQRImage = fetchQRCode;
			call( SHORTEN, {longUrl:url} );
		}
		
		public function expand(url:String):void
		{
			currentRequestType = EXPAND;
			getQRImage = false;
			call( EXPAND, {shortUrl:url} );
		}
		
		private function call(suffix:String, object:Object):void
		{
			var variables:URLVariables  = new URLVariables();
			variables.apiKey			= KEY;
			variables.login				= LOGIN;
			variables.format			= FORMAT;
			
			for(var params:String in object)
			{
				variables[params] = object[params];
			}
			
			var request:URLRequest = new URLRequest(REST + suffix);
			request.data = variables;
			
			if(dataLoader) dataLoader.clear();
			dataLoader = new DataLoader(request, {onComplete:onComplete});
			dataLoader.load();
		}
		
		public function clear():void
		{
			dataLoader.clear();
			dataLoader = null;
		}
		
		private function storeQRImage(content:*):void
		{
			tempObject.qrbitmap = content as Bitmap;
			dispatchComplete();
		}
		
		private function dispatchComplete():void
		{
			dispatchEvent(new Event(Event.COMPLETE));			
			if(properties)
			{
				if(properties.onComplete) properties.onComplete.apply(null, [tempObject]);
			}
		}
		
		/* handlers */
		
		private function onComplete(data:*):void
		{
			tempObject = null 
				
			try
			{
				tempObject = DataFormatter.formatData(XML(data).data.entry)[0];
				if(tempObject.url) tempObject.qrcode = tempObject.url + QRCODE;
			}
			catch(e:Error)
			{
				tempObject = DataFormatter.formatData(XML(data).data)[0];
				if(tempObject.url) tempObject.qrcode = tempObject.url + QRCODE;
			}
			
			tempObject.status = String(XML(data).status_txt);
			
			if(getQRImage)
			{
				if(qrCodeLoader) qrCodeLoader.clear();
				qrCodeLoader = new DisplayLoader(new URLRequest(tempObject.qrcode), {onComplete:storeQRImage});
				qrCodeLoader.load();
			}
			else
			{
				dispatchComplete();
			}
		}
	}
}